Component({
  mixins: [],
  data: {
    shop: {},
    name: '拆礼盒-分享有礼',
    start: '2020-01-01',
    end: '2020-01-31',
    instruction: '1.本活动严禁作弊|2.如对本活动有疑问，可以联系商铺电话',
    mainText: '欢迎来到xxx店铺拆礼盒活动',
    phone: '01086466630',
    max_limit: '1000',
    activityData: {},
    labelCol: { fixedSpan: 7 },
    wrapperCol: { span: 17 },

  },
  props: {
  },
  didMount(e) {
    
    this.loadShop();
    this.loadData();
    console.log(getApp().globalData)
  },
  didUpdate() { },
  didUnmount() { },
  methods: {
    onTimeChange(e) {
      let time = e.detail.value;
      this.setData({
        start: time[0],
        end: time[1]
      })
    },
    onChange(e) {
      let {
        detail: { value },
        currentTarget: { dataset }
      } = e;
      let { name } = dataset;
      this.setData({ [name]: value });
    },
    goMedia() {
      my.navigateTo({
        url: `/pages/imagewrapper/imagewrapper`
      });
    },
    async loadShop() {
      const { cloud } = getApp();
      try {
        const res = await cloud.topApi.invoke({
          api: 'taobao.shop.seller.get',
          authScope: '*',
          data: { fields: 'sid,title,pic_path' }
        });
        this.setData({
          shop: res.shop
        })
      } catch (e) {
        my.alert({ content: 'error ' + e.message })
      }
    },

    async handleSubmit() {
      if (this.data.name == '') {
        my.alert({
          content: '活动名称未设置'
        })
        return false;
      }
      if (this.data.start == '') {
        my.alert({
          content: '活动日期未设置'
        })
        return false;
      }
      if (this.data.end == '') {
        my.alert({
          content: '活动日期未设置'
        })
        return false;
      }
      if (this.data.instruction == '') {
        my.alert({
          content: '活动说明未设置'
        })
        return false;
      }
      if (this.data.mainText == '') {
        my.alert({
          content: '主题文字未设置'
        })
        return false;
      }
      if (this.data.phone == '') {
        my.alert({
          content: '客服电话未设置'
        })
        return false;
      }
      if (this.data.max_limit == '') {
        my.alert({
          content: '客服电话未设置'
        })
        return false;
      }
      let sendData = {
        shop_id: this.data.shop.sid,
        shop_logo: 'http://logo.taobao.com/shop-logo' + this.data.shop.pic_path,
        shop_title: this.data.shop.title,
        name: this.data.name,
        start: this.data.start,
        end: this.data.end,
        instruction: this.data.instruction,
        mainText: this.data.mainText,
        phone: this.data.phone,
        max_limit: this.data.max_limit
      };
      const { cloud } = getApp();
      const res = await cloud.application.httpRequest({
        path: '/index.php/index/qn/set',
        method: 'POST',
        params: sendData
      });
      my.alert({
        title: '温馨提示',
        content: '修改成功,请在手机端查看效果！'
      })


    },
    async loadData() {
      const { cloud } = getApp();
      const res = await cloud.application.httpRequest({
        path: '/index.php/index/qn/getset',
        method: 'GET',
        params:{shop_id:getApp().globalData.shop_id}
      })

      if (res) {
        this.setData({
          activityData: res,
          name: res.name,
          start: res.start,
          end: res.end,
          instruction: res.instruction,
          mainText: res.mainText,
          phone: res.phone,
          max_limit: res.max_limit
        })
      }
    }
  },
});

